<template>
    <div class="login-container">
        <div class="login-box">
            <h1>登录</h1>
            <el-form :model="form" :rules="rules" ref="dynamicValidateForm" label-width="100px" class="demo-dynamic">
                <el-form-item
                    prop="username"
                    label="姓名"
                >
                    <el-input v-model="form.username" placeholder="请输入姓名"></el-input>
                </el-form-item>
                <el-form-item
                    prop="password"
                    label="密码"
                >
                    <el-input v-model="form.password" placeholder="请输入密码" type="password"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('dynamicValidateForm')">提交</el-button>
                    <el-button @click="resetForm('dynamicValidateForm')">重置</el-button>
                    <a href="#" @click.prevent="goToEnroll">注册</a>
                </el-form-item>
            </el-form>
        </div>
        <router-view></router-view>
    </div>
</template>

<script>
export default {
    data() {
        return {
            form: {
                username: '',
                password: ''
            },
            rules: {
                username: [
                    { required: true, message: '请输入姓名', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' }
                ]
            }
        };
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.$axios.post('login/user', this.form)
                        .then(response => {
                            if (response.status === 200) {
                                sessionStorage.setItem('user', JSON.stringify(response.data));
                                this.$router.push('/front_index');
                            } else {
                                this.$message.error('登录失败，请检查用户名和密码');
                            }
                        })
                        .catch(error => {
                            console.error('登录请求出错:', error);
                            this.$message.error('登录请求出错，请稍后再试');
                        });
                }
            });
        },
        resetForm(formName) {
            this.form.username = '';
            this.form.password = '';
            this.$refs[formName].resetFields();
        },
        goToEnroll() {
            this.$router.push('/enroll');
        }
    }
};
</script>

<style scoped>
.login-container {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    background-image: url('/th.jpg');
    background-size: cover;
    background-repeat: no-repeat;
    background-position: center;
}

.login-box {
    background-color: rgba(255, 255, 255, 0.9);
    padding: 40px;
    border-radius: 15px;
    box-shadow: 0 10px 20px rgba(0, 0, 0, 0.15);
    width: 450px;
    text-align: center;
    transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.login-box:hover {
    transform: translateY(-5px);
    box-shadow: 0 15px 30px rgba(0, 0, 0, 0.2);
}

h1 {
    color: #1890ff;
    margin-bottom: 30px;
    font-size: 28px;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.el-form-item {
    margin-bottom: 25px;
}

.el-input {
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.el-button {
    margin-right: 15px;
    padding: 12px 28px;
    border-radius: 8px;
    font-size: 16px;
    transition: background-color 0.3s ease;
}

.el-button:hover {
    background-color: #096dd9;
}

a {
    color: #1890ff;
    text-decoration: none;
    margin-left: 10px;
    font-size: 16px;
    transition: color 0.3s ease;
}

a:hover {
    color: #096dd9;
    text-decoration: underline;
}

/* 错误提示样式 */
.el-form-item__error {
    color: #f56c6c;
    font-size: 12px;
    line-height: 1;
    padding-top: 4px;
    position: relative;
    top: 4px;
}
</style>